package com.lg.service.impl;

import java.util.Date;

import com.lg.dao.Criteria;
import com.lg.dao.Example;
import com.lg.dao.criteria.AngelUserCriteria;
import com.lg.model.AngelUser;
import com.lg.model.enums.CommonEnum.Del_Flag;
import com.lg.model.exception.BusinessException;
import com.lg.model.exception.OtherException;
import com.lg.serivce.AngelUserService;

public class AngelUserServiceImpl extends GenericServiceImpl<AngelUser>
		implements AngelUserService {

	@Override
	public boolean findUserIsExist(String regid) throws BusinessException,
			OtherException {
		Example<Criteria> example = new Example<Criteria>(
				AngelUserCriteria.class);
		AngelUserCriteria blogarticleCriteria = (AngelUserCriteria) example
				.createCriteria();
		blogarticleCriteria.andRegIdEqualTo(regid);
		blogarticleCriteria.andDelFlagEqualTo(Del_Flag.No.getValue());
		AngelUser u = null;
		try {
			u = getBaseDAO().getAngelUserDAO().selectObjectByExample(example);
		} catch (Exception e) {
			throw new OtherException(e.getMessage());
		}
		if (u == null) {
			return false;
		}
		return true;
	}

	@Override
	public boolean addUserinfo(AngelUser user) throws BusinessException,
			OtherException {
		if (user == null || user.getRegId() == null || "".equals(user.getRegId()))
			return false;
		if (findUserIsExist(user.getRegId())) {
			return false;
		}
		if(user.getIsAccept() == null){
			user.setIsAccept(Del_Flag.No.getValue());
		}
		user.setRegDt(new Date());
		try {
			getBaseDAO().getAngelUserDAO().insertSelective(user);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	@Override
	public AngelUser findUserByRegid(String regid) throws BusinessException,
			OtherException {
		Example<Criteria> example = new Example<Criteria>(AngelUserCriteria.class);
		AngelUserCriteria blogarticleCriteria = (AngelUserCriteria) example.createCriteria();
		blogarticleCriteria.andRegIdEqualTo(regid);
		blogarticleCriteria.andDelFlagEqualTo(Del_Flag.No.getValue());
		AngelUser u = null;
		try {
			u = getBaseDAO().getAngelUserDAO().selectObjectByExample(example);
		} catch (Exception e) {
			throw new OtherException(e.getMessage());
		}
        return u;
	}

	@Override
	public AngelUser findUserInLogin(String regid, String pwd)
			throws BusinessException, OtherException {
		Example<Criteria> example = new Example<Criteria>(AngelUserCriteria.class);
		AngelUserCriteria blogarticleCriteria = (AngelUserCriteria) example.createCriteria();
		blogarticleCriteria.andRegIdEqualTo(regid);
		blogarticleCriteria.andUserPwdEqualTo(pwd);
		blogarticleCriteria.andDelFlagEqualTo(Del_Flag.No.getValue());
		AngelUser u = null;
		try {
			u = getBaseDAO().getAngelUserDAO().selectObjectByExample(example);
		} catch (Exception e) {
			throw new OtherException(e.getMessage());
		}
		return u;
	}
}
